//
//  UpdateAlertView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/26.
//

#import "UpdateAlertView.h"


@implementation UpdateAlertView

-(instancetype)initWithFrame:(CGRect)frame andVersion:(NSString*)version{
    self = [super initWithFrame:frame];
    if(self){
        self.version = version;
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    NSString *str = [NSString stringWithFormat:@"找到新版本%@ 是否立即更新？",self.version];
    CGFloat h = [self getStringHeightWithText:str font:JPUIFontPingFangSCRegular(16) viewWidth:210];
    if(h < 44){
        h = 44;
    }
    __weak typeof(self)self_weak_ = self;
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@280);
        make.centerX.mas_equalTo(self_weak_);
        make.height.mas_equalTo(118+h);
        make.centerY.mas_equalTo(self_weak_);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = JPUIFontPingFangSCRegular(16);
    titleLabel.numberOfLines = 0;
    titleLabel.textColor = KFontBlackColor;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = str;
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@210);
        make.top.mas_equalTo(@35);
        make.centerX.mas_equalTo(bgView);
    }];
    
    CGFloat btn_w = 278/2.0;
    UIButton *cancelBtn = [[UIButton alloc]init];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@48);
        make.width.mas_equalTo(btn_w);
    }];
    
    UIButton *updateBtn = [[UIButton alloc]init];
    [updateBtn setTitle:@"立即更新" forState:UIControlStateNormal];
    [updateBtn setTitleColor:KThemeColor forState:UIControlStateNormal];
    updateBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [updateBtn addTarget:self action:@selector(updateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:updateBtn];
    [updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@48);
        make.width.mas_equalTo(btn_w);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = KLineColor;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@-48.5);
        make.height.mas_equalTo(0.5);
        make.left.right.mas_equalTo(@0);
    }];
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = KLineColor;
    [bgView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@0);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(@48);
        make.centerX.mas_equalTo(bgView);
    }];
    
}

-(void)cancelBtnClick{
    [self removeFromSuperview];
}

-(void)updateBtnClick{
    if(_updateBlock){
        _updateBlock();
    }
}



@end
